import FormattedMessageCustom from '@/components/FormattedMessageCustom/FormattedMessageCustom';
import ShowDetail from '@/components/ShowDetail/ShowDetail';
import { getCooperateDetail, orderCooperate } from '@/servers/api/order';
import { FormattedMessage, history, useIntl, useLocation } from '@umijs/max';
import { Button, Spin, Typography, message } from 'antd';
import { useEffect, useState } from 'react';
type IParams = {
    join_order_id: number;
    order_id: number;
    showBtn: number;
};
const Cooperate: React.FC = () => {
    const intl = useIntl();
    const location = useLocation();
    const state = location.state as IParams;
    const [loading, setLoading] = useState(false);
    const [orderInfo, setorderInfo] = useState();
    const [joinInfo, setjoinInfo] = useState();
    const [total, setTotal] = useState(0);
    const { Title, Text } = Typography;
    function getCooperateDetailFun() {
        getCooperateDetail({
            order_id: state.order_id,
            join_order_id: state.join_order_id,
        })
            .then((res) => {
                let total = 0;
                setLoading(false);
                setorderInfo(res.data.orderInfo);
                setjoinInfo(res.data.joinInfo); // 合作数据
                // 计算总金额
                if (res.data.orderInfo.type === 'goods') {
                    total = res.data.joinInfo.price * res.data.joinInfo.num;
                } else if (res.orderInfo.type === 'transport') {
                    if (res.joinInfo.release_info.release_type === '3') {
                        // 人力少货，重量*价格
                        total =
                            res.data.joinInfo.price *
                            res.data.joinInfo.product_weight;
                    } else {
                        total = res.data.joinInfo.price;
                    }
                } else if (res.orderInfo.type === 'crossborder') {
                    total = res.data.joinInfo.price;
                } else {
                    total = res.data.joinInfo.price;
                }
                setTotal(total);
            })
            .catch((err) => {
                console.log(err);
            });
    }
    function orderCooperateFun() {
        orderCooperate({
            order_id: state.order_id,
            join_order_id: state.join_order_id,
        })
            .then((res) => {
                message.success(
                    intl.formatMessage({ id: '操作成功，请等待后台审核' }),
                );
                setTimeout(() => {
                    history.push('/order');
                }, 2000);
            })
            .catch((err) => {
                console.log(err);
            });
    }
    function payCancel() {
        if (orderInfo?.release_info.pay_type.value === '1') {
            // 预付全款
            message.error(intl.formatMessage({ id: '请先预付全款' }));
        } else if (orderInfo?.release_info.pay_type.value === '2') {
            // 预付定金
            message.error(intl.formatMessage({ id: '请先预付定金' }));
        }
    }
    function payConfirm() {
        orderCooperateFun();
    }
    function doCooperate() {
        orderCooperateFun();
    }
    useEffect(() => {
        setLoading(true);
        getCooperateDetailFun();
    }, []);
    return (
        <Spin spinning={loading}>
            <ShowDetail
                info={joinInfo}
                releaseType="order_join"
                type={2}
                showBtn="false"
            ></ShowDetail>
            <Title level={5} style={{ textAlign: 'right', marginTop: 30 }}>
                <FormattedMessage id="总计"></FormattedMessage>：
                <Text type="danger">
                    {total}
                    <FormattedMessageCustom
                        id={joinInfo?.price_unit}
                    ></FormattedMessageCustom>
                </Text>
            </Title>
            <div style={{ textAlign: 'right' }}>
                {state.showBtn === 1 && (
                    <Button type="primary" onClick={doCooperate}>
                        <FormattedMessage id="确认合作"></FormattedMessage>
                    </Button>
                )}
            </div>
        </Spin>
    );
};
export default Cooperate;
